home *** CD-ROM | disk | FTP | other *** search
- Path: rz.uni-passau.de!piligrim!berndl
- From: berndl@piligrim.uni-passau.de (Klaus Berndl)
- Newsgroups: comp.lang.c
- Subject: Allocation of memory
- Date: 3 Feb 1996 14:28:25 GMT
- Organization: University of Passau, Germany
- Sender: berndl@piligrim (Klaus Berndl)
- Distribution: world
- Message-ID: <4evre9$8r4@news.rz.uni-passau.de>
- NNTP-Posting-Host: 132.231.1.72
- Keywords: realloc
-
- today i have a not-RPC-problem, but perhaps you can solve it:
-
- Please look at the foolwing short piece of code:
-
- #include <stdlib.h>
- #include <malloc.h>
-
- #define ALLOCSIZE 100000000
-
- extern char* strcpy();
-
- main () {
-
- char* t = NULL;
- char* temp = NULL;
- char* x = NULL;
-
- t = (char*)realloc(t, 1000);
- strcpy (t, "Klaus");
- printf("\n%s\n", t);
- temp = t;
- if ((t = (char*)realloc(t, ALLOCSIZE)) == NULL) {
- printf("\n t-realloc failed\n");
- t = temp;
- }
- printf("\ntest1\n");
- if ((x = (char*)realloc(x, 1000000)) == NULL) {
- printf("\n x-realloc failed\n");
- }
- printf("\ntest2\n");
- printf("\n%s\n", t);
- }
-
- Now my questions:
-
- 1) How is it possible, that my machine (Sparc classic) allocates 100MB
- Storage? I`m sure i have not so much main memory installed. Does it
- use swap-file??
- 2) With ALLOCSIZE set to 100000000 the program works correct. But if i set
- ALLOCSIZE to 300000000, i reach the following output:
-
- Klaus
-
- t-realloc failed
-
- test1
- Bus Error (core dumped)
-
- What is 'realloc' doing exactly? Normally if realloc can`t allocate the
- requested memory it returns NULL and doesn`t allocate memory. Why the
- second realloc-call (x = ...) fails? There must be 1000000 Byte memory
- because of the first try with ALLOCSIZE 100000000 works!
-
- 3) We are comming to the point: I must succesive realloc storage for a
- certain variable v. And if realloc fails, i want have the recent correct
- memory allocation for v containing the right value (look at 'temp' in the
- code). I need a save solution!
-
-
- Ciao,
- Klaus
-
- ____________________________________________________________________________
-
- Klaus Berndl e-mail: berndl@fmi.uni-passau.de
- ____________________________________________________________________________
-